﻿#region [ Modification History ]
/*
 * 06/04/2013 Denis Kholine
 *  Generated Original version of source code.
 */
#endregion

#region  [ UIUC NCSA Open Source License ]
/*****************************************************************************\
 *             University of Illinois/NCSA Open Source License
 *
 * 
 *      Copyright © 2012,2013, The Board of Trustees of the University of Illinois. All rights reserved.
 *
 *      Developed by: Rakesh Bobba, Derek Dagit, Erich Heine, Denis Kholine,
 *                    Klara Nahrstedt, David Rodgers,   William H. Sanders, Tim Yardley
 *
 *      Information Trust Institute/Coordinated Science Laboratory
 * 
 *      University of Illinois
 *      
 *      The Board of Trustees of the University of Illinois
 *      109 Coble Hall
 *      801 South Wright Street
 *      Champaign, IL 61820-6242
 *
 *      http://www.iti.illinois.edu / http://csl.illinois.edu
 * 
 *      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 *      associated documentation files (the “Software”), to deal with the Software without restriction, 
 *      including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *      and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 *      subject to the following conditions:
 *
 *      Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *      following disclaimers.
 *      
 *      Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 *      the following disclaimers in the documentation and/or other materials provided with the distribution.
 *
 *      Neither the names of Information Trust institute, Coordinated Science Laboratory, University of Illinois,
 *      The Board of Trustees of the University of Illinois nor the names of its contributors may be used to 
 *      endorse or promote products derived from this Software without specific prior written permission.
 *      
 *      THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 *      LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN
 *      NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *      WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 *      SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 *      
 \*****************************************************************************/
#endregion

#region [ Using ]
using System;
using System.Data;
using System.Linq;
using SIEGate.Core;
using SIEGate.Core.Fakes;
using SIEGate.Core.Notifications;
using SIEGate.Core.MessageBusses;
using SIEGate.Core.Configurator;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GSF;
using GSF.TimeSeries.Transport;
#endregion

namespace SIEGateCoreTest.Configurator
{
    /// <summary>
    /// A test class for AdapterBase.AdapterInfoBase subclass
    /// </summary>
    [TestClass]
    public class AdapterInfoBaseTests
    {
        #region [ Members]
        Dictionary<string, string> configInfo;
        private MockAdapter.AdapterInfoBase target; 
        private DataTable dtMeasurement;
        private DataRow dtMeasurementRow;
        private DataSet dtSet;
        private String dtTable = "ActiveMeasurements";
        private ISet<Guid> incoming0;
        private ISet<Guid> outgoing0;
        private Guid incoming;
        private Guid outgoing;
        private bool logevent;
        #endregion

        #region [ Test Initialization ]
        /// <summary>
        /// Initializa Active Measurement Table 
        /// </summary>
        [TestInitialize]
        public void Initialize()
        {
            String signalID = Guid.NewGuid().ToString();
            String pointTag = "GPA_PMU_34:21";

            dtSet = new DataSet("AdapterInfo");
            /**********************************************************************/
            // Populate measurement table
            dtMeasurement = new DataTable(dtTable);
            dtMeasurement.Columns.Add("ID");
            dtMeasurement.Columns.Add("SignalID");
            dtMeasurement.Columns.Add("PointTag");
            dtMeasurement.Columns.Add("Adder");
            dtMeasurement.Columns.Add("Multiplier");

            dtMeasurementRow = dtMeasurement.NewRow();
            dtMeasurementRow["ID"] = (10).ToString();
            dtMeasurementRow["SignalID"] = signalID; // Guid
            dtMeasurementRow["PointTag"] = pointTag; // "GPA_PMU_34:21";
            dtMeasurementRow["Adder"] = "UIUC\\dkholine";
            dtMeasurementRow["Multiplier"] = "0.033";

            dtMeasurement.Rows.Add(dtMeasurementRow);
            dtSet.Tables.Add(dtMeasurement);

            incoming = Guid.Parse("7C47D697-7563-4137-AAC3-18C87A44EACF");
            outgoing = Guid.Parse("FBA21A7A-B284-447C-A359-2040F751BD5B");

            configInfo = new Dictionary<string, string>();
            configInfo.Add("incomingStreamIDs", incoming.ToString());
            configInfo.Add("outgoingStreamIDs", outgoing.ToString());
            //data 
            incoming0 = new HashSet<Guid>();
            outgoing0 = new HashSet<Guid>();
            // Assign values
            incoming0.Add(incoming);
            outgoing0.Add(outgoing);
            // initialize signals
            INotificationReceiver signal0 = new MockINotificationReceiver();
            // processor info
            IAdapterInfo adapterinfo0 = new MockAdapter.MockAdapterInfo(incoming0, outgoing0);
            adapterinfo0.ID = (uint)1;
            adapterinfo0.Name = (String)"SimpleAdapter0";
            adapterinfo0.Receiver = signal0;
            target = (AdapterBase.AdapterInfoBase)adapterinfo0; 
            target.Update(configInfo);
        }
        #endregion

        #region [ Methods ]
        /// <summary>
        /// A AdapterInfoBase DefaultGet test
        /// </summary>
        [TestMethod]
        [TestCategory("AdapterInfoBase")]
        public void DefaultGetTest()
        {
            
            Assert.AreEqual("bar",target.DefaultGet("foo"));
            Assert.AreEqual("heine", target.DefaultGet("erich"));
            Assert.AreEqual(null, target.DefaultGet("stuff"));
        }

        /// <summary>
        /// A AdapterInfoBase ExtractConfiguration incoming stream test
        /// </summary>
        [TestMethod]
        [TestCategory("AdapterInfoBase")]
        public void ExtractConfigurationIncomingStreamTest()
        {
            Initialize();
            target.ExtractConfiguration(dtSet);
            bool expected = true;
            bool actual = (target.IncomingStreamIDs.Contains(incoming));
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// A AdapterInfoBase ExtractConfiguration outgoing stream test
        /// </summary>
        [TestMethod]
        [TestCategory("AdapterInfoBase")]
        public void ExtractConfigurationOutgoingStreamTest()
        {
            Initialize();
            target.ExtractConfiguration(dtSet);
            bool expected = true;
            bool actual = (target.OutgoingStreamIDs.Contains(outgoing));
            Assert.AreEqual(expected, actual); 
        }

        /// <summary>
        /// A AdapterInfoBase OnLog test
        /// </summary>
        [TestMethod]
        [TestCategory("AdapterInfoBase")]
        public void OnLogTest()
        {
            Initialize();
            target.Log +=target_Log;
            string format = "";
            logevent = false;
            bool expected = true;
            target.OnLog(format, dtSet);
            Assert.AreEqual(expected, logevent);
            target.Log -= target_Log;
        }
        public void target_Log(object sender,EventArgs<string> obj)
        {
            logevent = true;
        }

        /// <summary>
        /// A AdapterInfoBase SetDefaults test
        /// </summary>
        [TestMethod]
        [TestCategory("AdapterInfoBase")]
        public void SetDefaultsTest()
        {
            Initialize();
            Dictionary<string, string> defaults_in = new Dictionary<string, string>();
            string actual = "";
            defaults_in.Add("AssemblyName", "CsvAdapter.dll");
            defaults_in.Add("AssemblyType", "AdapterInfo");
            target.SetDefaults(defaults_in);
            actual  = target.DefaultGet("AssemblyName");
            string expected = "CsvAdapter.dll";
            Assert.AreEqual(expected, actual);
            defaults_in.Clear();
        }

        /// <summary>
        /// A AdapterInfoBase RuntimeType test
        /// </summary>
        [TestMethod]
        [TestCategory("AdapterInfoBase")]
        public void RuntimeGetTypeTest()
        {
            Initialize();
            Type runtimetype = typeof(AdapterBase);
            Assert.AreEqual(runtimetype, target.RuntimeType);
        }

        /// <summary>
        /// A AdapterInfoBase RuntimeType test
        /// </summary>
        [TestMethod]
        [TestCategory("AdapterInfoBase")]
        public void RuntimeSetTypeTest()
        {
            Initialize();
            Type runtimetype = typeof(AdapterBase);
            target.RuntimeType = runtimetype;
            Assert.AreEqual(runtimetype, target.RuntimeType);
        }
        #endregion
    }
}